<template>
  <div>
    <el-row>
      <el-col>
        <el-button
          type="primary"
          size="mini"
          @click="add"
          :disabled="(nowRoot === '学科带头人' && this.isAdd)"
          v-if="nowRoot === '学科带头人'"
          >添加(一年只能有一个)</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <my-table
          :hasBorder="true"
          :hasIndex="true"
          :hasPagination="false"
          :hasSelection="false"
          :hasStripe="true"
          :tableColumnOptions="tableColumnOptions"
          :tableData="tableData"
        >
          <template #peState="{ data }">
            {{
              data.peState == "-1"
                ? "未通过"
                : data.peState == "0"
                ? "未审核"
                : "审核通过"
            }}
          </template>
          <template #peTime="{ data }">
            {{ (data.peTime + "").slice(0, 10) }}
          </template>
          <template #operation="{ data }">
            <el-button type="text" size="mini" @click="downloadPDF(data)"
              >查看pdf文件</el-button
            >
            <el-button type="text" size="mini" @click="update(data)"
              >编辑</el-button
            >
            <el-popconfirm
              confirm-button-text="审核通过"
              cancel-button-text="审核不通过"
              confirm-button-type="success"
              cancel-button-type="danger"
              @confirm="check_T(data)"
              @cancel="check_F(data)"
              icon="el-icon-info"
              icon-color="red"
              title="请选择操作"
            >
              <el-button
                slot="reference"
                icon="el-icon-coordinate"
                type="primary"
                size="mini"
                v-if="nowRoot === '校管理员'"
                >批量审核</el-button
              >
            </el-popconfirm>
          </template>
        </my-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import request from "@/utils/request";
import myTable from "@/components/myTable.vue";
import { A_tableData } from "@/api/performanceEvaluation";
import cloneDeep from "lodash/cloneDeep";
import { mapState } from "vuex";
export default {
  name: "myProject",
  inject: ["reload"],
  data() {
    return {
      tableColumnOptions: [
        {
          isSlot: true,
          slotName: "peTime",
          label: "年度时间",
          prop: "peTime",
          width: "200",
        },
        {
          label: "部门",
          prop: "peDepartment",
          width: "200", 
        },
        {
          label: "学科",
          prop: "peSub",
          width: "200",
        },
        {
          isSlot: true,
          slotName: "peState",
          label: "审核状态",
          prop: "peState",
          width: "200",
        },
        {
          label: "操作",
          isSlot: true,
          slotName: "operation",
          width: "250",
          fixed: "right",
        },
      ],
      tableData: [],
      isAdd: false,
    };
  },
  components: {
    myTable,
  },
  created() {
    this.getTableData();
  },
  methods: {
    async getTableData() {
      let result = await A_tableData();
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
      if (this.tableData.length > 0) {
        let fileTime = parseInt(this.tableData[0].peTime.slice(0, 4));
        let nowTime = new Date().getFullYear();
        if (fileTime) {
          if (nowTime > fileTime) {
            this.isAdd = true;
          } else {
            this.isAdd = false;
          }
        }
      }
    },
    add() {
      this.$router.push({
        name: "WriteNewPerformance",
        params: { isedit: false },
      });
    },
    update(data) {
      this.$router.push({
        name: "WriteNewPerformance",
        params: {
          isedit: true,
          time: data.peTime,
          department: data.peDepartment,
          subject: data.peSub,
        },
      });
    },
    async check_T(data) {
      await request({
        url: "/PExam/Tshen",
        method: "post",
        data: {
          peTime: data.peTime,
          peDepartment: data.peDepartment,
          peSub: data.peSub,
        },
        headers: {
          "Content-type": "application/json;charset=utf-8",
        },
      });
      this.reload();
    },
    async check_F(data) {
      await request({
        url: "/PExam/Nshen",
        method: "post",
        data: {
          peTime: data.peTime,
          peDepartment: data.peDepartment,
          peSub: data.peSub,
        },
        headers: {
          "Content-type": "application/json;charset=utf-8",
        },
      });
      this.reload();
    },
    downloadPDF(data) {
      request({
        url: "/Pdf/P",
        method: "post",
        data: {
          peTime: data.peTime,
          peDepartment: data.peDepartment,
          peSub: data.peSub,
        },
        responseType: "blob",
      }).then((res) => {
        const url = window.URL.createObjectURL(
          new Blob([res], { type: "application/pdf" })
        );
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", "pdf文件");
        document.body.appendChild(link);
        link.click();
      });
    },
  },
  computed: {
    ...mapState("myInfo", ["nowRoot"]),
  },
};
</script>

<style>
</style>